package com.zhenhe.core.common.enums;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.zhenhe.core.common.enums.annotation.EnumOnly;
import com.zhenhe.core.common.utils.StringUtils;

public class EnumValidation implements ConstraintValidator<EnumOnly, Object> {

	private String enumName;
	
	@Override
	public void initialize(EnumOnly constraintAnnotation) {
		enumName = constraintAnnotation.enumName();
	}

	@Override
	public boolean isValid(Object value, ConstraintValidatorContext context) {
		Integer code = (Integer) value;
		String label = EnumUtils.getEnumLabel(enumName, code);
		if(StringUtils.isNotBlank(label)){
			return true;
		}
		return false;
	}

}
